//
//  NIFilletedCornerButton.m
//  AIWAYS
//
//  Created by Marx on 2018/6/12.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NIFilletedCornerButton.h"
#import "ColorDefine.h"
@interface NIFilletedCornerButton()
{
    CAGradientLayer *layer;
}

@end

@implementation NIFilletedCornerButton


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    if (_backGroundColor) {
        [self setBackgroundColor:self.backGroundColor];
    }
    else
    {
        if (layer) {
            [layer removeFromSuperlayer];
            layer = nil;
        }
        if (self.enabled) {
            layer = [NIFilletedCornerButton setGradualChangingColor:self fromColor:[UIColor colorWithRed:0.0/255.0 green:199.0/255.0 blue:232.0/255.0 alpha:1.0] toColor:[UIColor colorWithRed:67.0/255.0 green:188.0/255.0 blue:222.0/255.0 alpha:1.0]];
        }
        else
        {
            
            layer = [NIFilletedCornerButton setGradualChangingColor:self fromColor:[UIColor colorWithRed:215.0/255.0 green:223.0/255.0 blue:231.0/255.0 alpha:1.0] toColor:[UIColor colorWithRed:215.0/255.0 green:223.0/255.0 blue:231.0/255.0 alpha:1.0]];
        }
        [self.layer insertSublayer:layer atIndex:0];
        
        [self setTitleColor:[UIColor colorWithRed:254.0/255.0 green:254.0/255.0 blue:254.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        
    }
    
    
    self.layer.cornerRadius = self.frame.size.height/2.0; 
    self.layer.masksToBounds = YES;
}


//绘制渐变色颜色的方法
+ (CAGradientLayer *)setGradualChangingColor:(UIView *)view fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor{
    
    //    CAGradientLayer类对其绘制渐变背景颜色、填充层的形状(包括圆角)
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(__bridge id)fromColor.CGColor,(__bridge id)toColor.CGColor];
    
    //  设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    
    //  设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@0,@1];
    
    return gradientLayer;
}

-(void)setEnabled:(BOOL)enabled
{
    [super setEnabled:enabled];
    if (_disableBackGroundColor) {
        [self setBackgroundColor:_disableBackGroundColor];
        return;
    }
    if (_backGroundColor) {
        return;
    }
    if (layer) {
        [layer removeFromSuperlayer];
        layer = nil;
    }
    if (self.enabled) {
        layer = [NIFilletedCornerButton setGradualChangingColor:self fromColor:[UIColor colorWithRed:0.0/255.0 green:199.0/255.0 blue:232.0/255.0 alpha:1.0] toColor:[UIColor colorWithRed:67.0/255.0 green:188.0/255.0 blue:222.0/255.0 alpha:1.0]];
    }
    else
    {
        
        layer = [NIFilletedCornerButton setGradualChangingColor:self fromColor:[UIColor colorWithRed:215.0/255.0 green:223.0/255.0 blue:231.0/255.0 alpha:1.0] toColor:[UIColor colorWithRed:215.0/255.0 green:223.0/255.0 blue:231.0/255.0 alpha:1.0]];
    }
    [self.layer insertSublayer:layer atIndex:0];
}


@end
